import streamlit as st
from datetime import datetime

class SidebarView:
    """侧边栏视图类"""
    
    def __init__(self):
        pass
    
    def render_menu(self, is_processing: bool, on_start_callback, on_stop_callback):
        """渲染侧边栏菜单"""
        with st.sidebar:
            st.markdown("<h1 class='sidebar-title'>EmlAIdata 邮件处理监控面板</h1>", unsafe_allow_html=True)
            
            col1, col2 = st.columns(2)
            
            with col1:
                if st.button(
                    "开始" if not is_processing else "处理中",
                    disabled=is_processing,
                    use_container_width=True,
                    type="primary" if not is_processing else "secondary"
                ):
                    if on_start_callback:
                        on_start_callback()
            
            with col2:
                if st.button(
                    "停止",
                    disabled=not is_processing,
                    use_container_width=True,
                    type="secondary"
                ):
                    if on_stop_callback:
                        on_stop_callback()
            
            
            st.markdown("---")
            
            # Dashboard菜单
            if st.button("📊 Dashboard", use_container_width=True, key="menu_dashboard"):
                st.session_state.current_menu = "dashboard"
                st.session_state.current_submenu = None
                st.rerun()
            
            # 配置管理菜单
            if st.button("⚙️ 配置管理", use_container_width=True, key="menu_config"):
                st.session_state.current_menu = "config"
                if st.session_state.get('current_submenu') is None:
                    st.session_state.current_submenu = 'email_settings'
                st.rerun()
            
            # 配置管理子菜单
            if st.session_state.get('current_menu') == 'config':
                col1, col2, col3 = st.columns([0.1, 0.8, 0.1])
                with col2:
                    if st.button("📧 邮箱设置", key="submenu_email", use_container_width=True):
                        st.session_state.current_submenu = 'email_settings'
                        st.rerun()
                    
                    if st.button("🤖 模型配置", key="submenu_model", use_container_width=True):
                        st.session_state.current_submenu = 'model_config'
                        st.rerun()
                    
                    if st.button("📄 配置文件", key="submenu_files", use_container_width=True):
                        st.session_state.current_submenu = 'config_files'
                        st.rerun()
            
            # 其他菜单项
            menu_items = [
                ("📋 结果查询", "results", "📊"),
                ("📝 实时日志", "logs", "📋"),
                ("📈 结果分析", "result_analysis", "📊"),
                ("⚠️ 错误记录", "error_records", "⚠️"),
                ("✅ 数据核验", "data_verification", "🔍")
            ]
            
            for label, key, icon in menu_items:
                if st.button(label, use_container_width=True, key=f"menu_{key}"):
                    st.session_state.current_menu = key
                    st.session_state.current_submenu = None
                    st.rerun()